package com.huawei.hianalytics.ab.bc.de;

import android.text.TextUtils;
import com.huawei.hianalytics.ab.bc.cd.bc.de;
import com.huawei.hianalytics.ab.bc.cd.bc.ef;
import com.huawei.hianalytics.ab.bc.ef.ab;
import com.huawei.hianalytics.ab.bc.kl.bc;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class cd {
    private String ab;
    private String bc;
    private String cd;
    private List<de> de;
    private String ef;

    public cd(String str, String str2, String str3, List<de> list, String str4) {
        this.ab = str;
        this.bc = str2;
        this.cd = str3;
        this.de = list;
        this.ef = str4;
    }

    private String ab(String str, String str2) {
        CharSequence cd = com.huawei.hianalytics.ab.ab.cd.cd(str, str2);
        if (TextUtils.isEmpty(cd)) {
            ab.ab("DataReportHandler", "No report address,TAG : %s,TYPE: %s ", str, str2);
            return "";
        }
        return "oper".equals(str2) ? "{url}/common/hmshioperqrt".replace("{url}", cd) : "maint".equals(str2) ? "{url}/common/hmshimaintqrt".replace("{url}", cd) : "diffprivacy".equals(str2) ? "{url}/common/common2".replace("{url}", cd) : "";
    }

    private byte[] ab(ef efVar) {
        try {
            JSONObject cd = efVar.cd();
            if (cd != null) {
                return com.huawei.hianalytics.ab.bc.kl.de.ab(cd.toString().getBytes("UTF-8"));
            }
            ab.cd("DataReportHandler", "uploadEvents is null");
            return new byte[0];
        } catch (JSONException e) {
            ab.cd("DataReportHandler", "uploadEvents to json error");
            return new byte[0];
        } catch (UnsupportedEncodingException e2) {
            ab.cd("DataReportHandler", "sendData(): getBytes - Unsupported coding format!!");
            return new byte[0];
        }
    }

    private JSONArray bc() {
        JSONArray jSONArray = new JSONArray();
        for (de de : this.de) {
            try {
                jSONArray.put(de.de());
            } catch (JSONException e) {
                ab.bc("DataReportHandler", "handleEvents: json error,Abandon this data");
            }
        }
        return jSONArray;
    }

    private void cd() {
        if (bc.ab(com.huawei.hianalytics.ab.ab.bc.ik(), "backup_event", 5242880)) {
            ab.bc("DataReportHandler", "backup file reach max limited size, discard new event ");
            return;
        }
        JSONArray bc = bc();
        String ab = com.huawei.hianalytics.ab.bc.kl.ef.ab(this.ab, this.bc, this.ef);
        ab.bc("DataReportHandler", "Update data cached into backup,spKey: " + ab);
        com.huawei.hianalytics.ab.bc.ij.ab.ab(com.huawei.hianalytics.ab.ab.bc.ik(), "backup_event", ab, bc.toString());
    }

    private ef de() {
        return com.huawei.hianalytics.ab.de.ab.ab(this.de, this.ab, this.bc, this.ef, this.cd);
    }

    public void ab() {
        Object ab = ab(this.ab, this.bc);
        if (!TextUtils.isEmpty(ab) || "preins".equals(this.bc)) {
            if (!("_hms_config_tag".equals(this.ab) || "_openness_config_tag".equals(this.ab))) {
                cd();
            }
            ef de = de();
            if (de != null) {
                byte[] ab2 = ab(de);
                if (ab2.length == 0) {
                    ab.cd("DataReportHandler", "request body is empty");
                    return;
                }
                com.huawei.hianalytics.ab.bc.ik.cd.bc.bc().ab(new com.huawei.hianalytics.ab.bc.ik.bc.ab(ab2, ab, this.ab, this.bc, this.ef, this.de));
                return;
            }
            com.huawei.hianalytics.ab.bc.ik.cd.bc.ab().ab(new com.huawei.hianalytics.ab.bc.ik.bc.de(this.de, this.ab, this.ef, this.bc));
            return;
        }
        ab.cd("DataReportHandler", "collectUrl is empty");
    }
}
